<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Taichinh extends MY_Controller {

	public $data 	= 	array();
 	public $_template = array();
	public function __construct() {

		parent::__construct(); //for CI 2.x users 
		$this->load->module_model('member', 'taichinh_model', 'taichinh');
		$this->load->module_model('member', 'store_model', 'store');
		$this->load->helper('text');
		$this->user = $this->access->get_user();
		$this->load->helper('url');
		$this->access->restrict();
		$this->load->dbutil();
	}
	
	/*
	 * Bổ sung: ManhVT 27/10/2014
	 * Thêm chức năng áp giá cho phòng tài chính
	 */
	public function submit_item_in_store()
	{
		$id = $this->input->post('id');
		$return_id = $this->store->submit_item_in_store($id,3);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	public function saveiteminodastore()
	{
		$id = $this->input->post('id');
		$amount= $this->input->post('amount');
		$price= $this->input->post('price');
		if($this->store->saveiteminstore($id,$amount,$price))
		{
			$status = 1;
		}
		else 
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status));
	}
	
	public function viewitem($id=null)
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Chi tiết đầu kỳ của kho';
		$data['description'] = 'Chi tiết đầu kỳ của kho';
		$itemeds = $this->store->get_all_sddked_item_by_storeid($id);
		$data['store_info']	=	$this->store->get_store($id);
		$data['stores']	=	$this->store->get_all_store();
		$data['listdata'] = $itemeds;
		//var_dump($data['listdata']);
		$this->_template['page']	= 'users/taichinh/store/chitietdauky';
		$this->system_library->load($this->_template['page'], $data);
	}
	public function list_store() 
	{
		//$this->access->restrict('Member.Hocvien.Index');
		$data['ItemID'] = 'User';
		$data['title'] = 'Quản lý Nhà Kho, Xưởng';
		$data['description'] = 'Quản lý Nhà Kho, Xưởng';
	
		$this->access->restrict();
		$this->load->library('basic_library');
		$this->load->library('pagination');
	
		$config = array();
		$config['base_url'] = base_url().'member/taichinh/list_store';
		$config['total_rows'] = $this->store->count_all_store();
		$config['per_page'] = 50;
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu';
		$config['last_link'] = 'Cuối';
		$config['next_link'] = 'Tiến';
		$config['prev_link'] = 'Lùi';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['listdata'] = $this->store->get_list_store($config['per_page'], $page);
		$data['allstore'] = $this->store->get_all_store();
		$data['links'] = $this->pagination->create_links();
		$this->_template['page']	= 'users/taichinh/store/list';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function do_exp_excel($type,$duration=null)
	{
		
		if($type == 1)
		{
			//echo $type;
			$listdata	=	$this->taichinh->get_all_chung_tu_ok($duration);
			$tong = $this->taichinh->tinh_tong_ps($duration);
			//var_dump($tong);
			$this->load->library('exzel');
			//activate worksheet number 1
			$this->exzel->setActiveSheetIndex(0);
			//name the worksheet
			$this->exzel->getActiveSheet()->setTitle('Bao cao chi tiet');
			//set cell A1 content with some text
			$this->exzel->getActiveSheet()->setCellValue('A2', 'Báo cáo chi tiết');
			
			$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(32);
			$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(22);
			$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(22);
			$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
			$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(22);
			
			$this->exzel->getActiveSheet()->setCellValue('A3', 'Ngày tháng');
			$this->exzel->getActiveSheet()->setCellValue('B3', 'Hạng mục');
			$this->exzel->getActiveSheet()->setCellValue('C3', 'Phát sinh tăng');
			$this->exzel->getActiveSheet()->setCellValue('D3', 'Phát sinh giảm');
			
			$this->exzel->getActiveSheet()->setCellValue('A4', 'Tổng');
			$this->exzel->getActiveSheet()->setCellValue('B4', '');
			$this->exzel->getActiveSheet()->setCellValue('C4', $tong[0]['tang']);
			$this->exzel->getActiveSheet()->setCellValue('D4',  $tong[0]['giam']);
			$this->exzel->getActiveSheet()->getStyle("C4")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			$this->exzel->getActiveSheet()->getStyle("D4")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
			
			$i = 1;
			foreach($listdata as $item)
			{
				
				$j = 4 + $i;
				$this->exzel->getActiveSheet()->setCellValue("A$j", date('d/m/Y',strtotime($data['created_date'])));
				$this->exzel->getActiveSheet()->setCellValue("B$j", $item['item']);
				$this->exzel->getActiveSheet()->setCellValue("C$j", number_format($item['ps_tang']));
				$this->exzel->getActiveSheet()->setCellValue("D$j", number_format($item['ps_giam']));
				//$this->exzel->getActiveSheet()->setCellValueExplicit("B$i", "23123734823493478", PHPExcel_Cell_DataType::TYPE_STRING);
				$this->exzel->getActiveSheet()->getStyle("C$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
				$this->exzel->getActiveSheet()->getStyle("D$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
				$this->exzel->getActiveSheet()->getStyle("E$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
				$this->exzel->getActiveSheet()->getStyle("F$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
				$i++;
			}
			
			$filename='bao-cao-chi-tiet.xls'; //save our workbook as this file name
		}
		else 
		{
			$rets = $this->taichinh->do_exp_excel($duration);
			//var_dump($rets);
			if($rets != null)
			{
				$tong_hops = $rets['tong_hop'];
				$nguons = $rets['nguon'];			
				//var_dump($tong_hops); exit();
				//load our new PHPExcel library
				$this->load->library('exzel');
				//activate worksheet number 1
				$this->exzel->setActiveSheetIndex(0);
				//name the worksheet
				$this->exzel->getActiveSheet()->setTitle('test worksheet');
				//set cell A1 content with some text
				$this->exzel->getActiveSheet()->setCellValue('A7', 'Báo cáo tổng hợp quỹ');
				$i = 10;
				$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(32);
				$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(22);
				$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(22);
				$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(22);
				$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(22);
				foreach($tong_hops as $item)
				{
					$totalall = 0;
					$totalall = $item['sddk'] + $item['ps_tang'] - $item['ps_giam'];
					$this->exzel->getActiveSheet()->setCellValue("A$i", $item['item']);
					$this->exzel->getActiveSheet()->setCellValue("B$i", number_format($totalall));
					//$this->exzel->getActiveSheet()->setCellValueExplicit("B$i", "23123734823493478", PHPExcel_Cell_DataType::TYPE_STRING);
					$this->exzel->getActiveSheet()->getStyle("B$i")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$i++;
				}
			
				// Report quy phat sinh trong ky
				$this->exzel->getActiveSheet()->setCellValue('A14', 'Báo cáo quỹ phát sinh trong kỳ');
				$this->exzel->getActiveSheet()->setCellValue('A15', 'STT');
				$this->exzel->getActiveSheet()->setCellValue('B15', 'Hạng mục');
				$this->exzel->getActiveSheet()->setCellValue('C15', 'SDĐK');
				$this->exzel->getActiveSheet()->setCellValue('D15', 'Phát sinh tăng');
				$this->exzel->getActiveSheet()->setCellValue('E15', 'Phát sinh giảm');
				$this->exzel->getActiveSheet()->setCellValue('F15', 'SDCK');
				$i = 1;
				foreach($tong_hops as $item)
				{
					$totalall = 0;
					$j = 15 + $i;
					$this->exzel->getActiveSheet()->setCellValue("A$j", $i);
					$this->exzel->getActiveSheet()->setCellValue("B$j", $item['item']);
					$this->exzel->getActiveSheet()->setCellValue("C$j", number_format($item['sddk']));
					$this->exzel->getActiveSheet()->setCellValue("D$j", number_format($item['ps_tang']));
					$this->exzel->getActiveSheet()->setCellValue("E$j", number_format($item['ps_giam']));
					$this->exzel->getActiveSheet()->setCellValue("F$j", number_format($item['sdck']));
					//$this->exzel->getActiveSheet()->setCellValueExplicit("B$i", "23123734823493478", PHPExcel_Cell_DataType::TYPE_STRING);
					$this->exzel->getActiveSheet()->getStyle("C$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$this->exzel->getActiveSheet()->getStyle("D$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$this->exzel->getActiveSheet()->getStyle("E$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$this->exzel->getActiveSheet()->getStyle("F$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$i++;
				}
				
				
				// report theo hạng mục
				
				$this->exzel->getActiveSheet()->setCellValue('A20', 'Báo cáo theo hạng mục');
				$this->exzel->getActiveSheet()->setCellValue('A21', 'STT');
				$this->exzel->getActiveSheet()->setCellValue('B21', 'Hạng mục');
				$this->exzel->getActiveSheet()->setCellValue('C21', 'SDĐK');
				$this->exzel->getActiveSheet()->setCellValue('D21', 'Phát sinh tăng');
				$this->exzel->getActiveSheet()->setCellValue('E21', 'Phát sinh giảm');
				$this->exzel->getActiveSheet()->setCellValue('F21', 'SDCK');
				$i = 1;
				foreach($nguons as $item)
				{
					$totalall = 0;
					$j = 21 + $i;
					$this->exzel->getActiveSheet()->setCellValue("A$j", $i);
					$this->exzel->getActiveSheet()->setCellValue("B$j", $item['item']);
					$this->exzel->getActiveSheet()->setCellValue("C$j", number_format($item['sddk']));
					$this->exzel->getActiveSheet()->setCellValue("D$j", number_format($item['ps_tang']));
					$this->exzel->getActiveSheet()->setCellValue("E$j", number_format($item['ps_giam']));
					$this->exzel->getActiveSheet()->setCellValue("F$j", number_format($item['sdck']));
					//$this->exzel->getActiveSheet()->setCellValueExplicit("B$i", "23123734823493478", PHPExcel_Cell_DataType::TYPE_STRING);
					$this->exzel->getActiveSheet()->getStyle("C$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$this->exzel->getActiveSheet()->getStyle("D$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$this->exzel->getActiveSheet()->getStyle("E$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$this->exzel->getActiveSheet()->getStyle("F$j")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
					$i++;
				}
				//change the font size
				$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setSize(14);
				$this->exzel->getActiveSheet()->getStyle('E7')->getFont()->setSize(18);
				//make the font become bold
				$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setBold(false);
				//merge cell A1 until D1
				//$this->exzel->getActiveSheet()->mergeCells('A7:A9');
				//set aligment to center for that merged cell (A1 to D1)
				$this->exzel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				
				//$this->exzel->getActiveSheet()->mergeCells('B7:B9');
				//$this->exzel->getActiveSheet()->mergeCells('C7:C9');
				//$this->exzel->getActiveSheet()->mergeCells('D7:D9');
				//$this->exzel->getActiveSheet()->mergeCells('E7:P7');
				$filename='bao-cao-tong-hop.xls'; //save our workbook as this file name
			}
		}
			
			header('Content-Type: application/vnd.ms-excel'); //mime type
			header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
			header('Cache-Control: max-age=0'); //no cache
				
			//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
			//if you want to save it as .XLSX Excel 2007 format
			$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
			//force user to download the Excel file without writing it to server's HD
			//$objWriter->save("nameoffile.xls");
			$objWriter->save('php://output');
			exit();
		
	}
	// Export Excel layout to export
	public function export_excel()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Kết xuất file Excel';
		$data['description'] = 'Kết xuất file Excel';
		
		$this->_template['page']	= 'users/taichinh/excel';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	// tạo report tong hop
	public function tao_bc_tong_hop()
	{
		$id = $this->input->post('id');
		$return_id = $this->taichinh->tao_bc_tong_hop();
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	// Xuat ra Excel
	public function xuat_report_tong_hop($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('begin_date');
			$type =   strtoupper($this->input->post('select_type_report'));
			if($bdate == '')
			{
				$listdata['data']	=	$this->taichinh->get_report_tong_hop($type);
				$listdata['nguon']	=	$this->taichinh->get_report_tong_hop_nguon($type);
				$filename = 'bao_cao_tong_hop_'.date('d-m-Y');
				$title = 'Báo cáo tổng hợp '.date('d-m-Y');
			}
			else
			{
				$duration = $duration.'_'.$bdate;
				$filename = 'Bao_cao_tong_hop_'.$bdate;
				$title ='Báo cáo tổng hợp '.$bdate;
				
				$listdata['data']	=	$this->taichinh->get_report_tong_hop($duration);
				$listdata['nguon']	=	$this->taichinh->get_report_tong_hop_nguon($duration);
			}
			//$duration = $duration.'_'.$bdate.'_'.$edate;
			//$listdata	=	$this->xangdau->get_report_tong_hop_xe($duration);
			//var_dump($listdata);
			if($listdata)
			{
				$return = '';
				$return = $this->generate_excel($listdata,$filename,$title);
			}
				
		}
	}
	
	public function generate_excel($listdata,$filename,$title)
	{
		$arrT = array();
		$arrT[1] = 'A';
		$arrT[2] = 'B';
		$arrT[3] = 'C';
		$arrT[4] = 'D';
		$arrT[5] = 'E';
		$arrT[6] = 'F';
		$arrT[7] = 'G';
		$arrT[8] = 'H';
		$arrT[9] = 'I';
		$arrT[10] = 'J';
		$arrT[11] = 'K';
		$arrT[12] = 'L';
		$arrT[13] = 'M';
		$arrT[14] = 'N';
		$arrT[15] = 'O';
		$arrT[16] = 'P';
		$arrT[17] = 'Q';
		$arrT[18] = 'R';
		$arrT[19] = 'S';
		$arrT[20] = 'T';
		$arrT[21] = 'U';
		$arrT[22] = 'V';
		$arrT[23] = 'W';
		$arrT[24] = 'X';
		$arrT[25] = 'Y';
		$arrT[26] = 'Z';
		$arrT[27] = 'AA';
		$arrT[28] = 'AB';
		$arrT[29] = 'AC';
		$arrT[30] = 'AD';
		$arrT[31] = 'AE';
		$arrT[32] = 'AF';
		$arrT[33] = 'AG';
		$arrT[34] = 'AH';
		$arrT[35] = 'AI';
		$arrT[36] = 'AK';
		$arrT[37] = 'AL';
		
			
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao_tong_hop');
		//set cell A1 content with some text
		$this->exzel->getActiveSheet()->setCellValue('A1', $title);
		
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		// Xử lý mảng
		$row = 6;
		$col = 1; //A col
		$data = $listdata['data'];
		$nguon = $listdata['nguon'];
		// Print Current Balance of System 
		// Display title of area Balance at Row 6
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Báo cáo quỹ");
		// Chuyển xuống dòng tiếp theo row + 1 --> dong thu 7
		$row++;
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
		$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
		$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
		$this->exzel->getActiveSheet()->getColumnDimension('F')->setWidth(25);
		if($data != null)
		{
			foreach ($data as $item)
			{
				$totalall = $item['sddk'] + $item['ps_tang'] - $item['ps_giam'];
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['item']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($totalall));
				$this->exzel->getActiveSheet()->getStyle('C'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
					
				$row++;
			}
			// Display title of area
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Báo cáo phát sinh trong kỳ");
			$row++; // chuyển đến dòng tiêp theo headline of area
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tên hạng mục");
			$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Số dư đầu kỳ");
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Phát sinh tăng");
			$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Phát sinh giảm");
			$this->exzel->getActiveSheet()->setCellValue('F'.$row, "Số dư cuối kỳ");
			$row++; // chuyển đến dòng tiêp theo headline of area
			$stt = 1;
			foreach ($data as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['item']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($item['sddk']));
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($item['ps_tang']));
				$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($item['ps_giam']));
				$this->exzel->getActiveSheet()->setCellValue('F'.$row, number_format($item['sdck']));
				$this->exzel->getActiveSheet()->getStyle('C'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				$this->exzel->getActiveSheet()->getStyle('E'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				$this->exzel->getActiveSheet()->getStyle('F'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
					
				$stt++;
				$row++;
			}
		}
		// Display title of area 
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Báo cáo theo hạng mục");
		$row++; // chuyển đến dòng tiêp theo headline of area
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
		$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tên hạng mục");
		$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Phát sinh tăng");
		$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Phát sinh giảm");
		$row++; // chuyển đến dòng tiêp theo headline of area
		$stt = 1;
		if($nguon != null)
		{
			foreach ($nguon as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, $item['name']);
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($item['ps_tang']));
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($item['ps_giam']));
				$this->exzel->getActiveSheet()->getStyle('C'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				
				$stt++;
				$row++;
			}
		}
		/*
		foreach ($data as $item)
		{
			$temp = $item['chungtus'];
			$stt = 1;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, $item['name']);
			$row++;
			$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'TT');
			$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, 'Hạng mục');
			$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(10);
			$this->exzel->getActiveSheet()->setCellValue('C'.$row, 'Nhận (Lít)');
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, 'Thành tiền');
			$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
			$row++;
			$jtotal = $row;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'Nhà trường');
			$row++;
			//print data
			$total = 0;
			$amount = 0;
			foreach ($temp as $obj)
			{
				if($obj['tbname'] !='KH')
				{
					$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
					$this->exzel->getActiveSheet()->setCellValue('B'.$row, $obj['tbname']);
					$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($obj['total'],2,'.',','));
					$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($obj['amount'],0,'.',','));
					$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
					$total = $total + 	$obj['total'];
					$amount = $amount + $obj['amount'];
					$stt++;
					$row++;
				}
			}
				
			$this->exzel->getActiveSheet()->setCellValue('C'.$jtotal, number_format($total,2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('D'.$jtotal, number_format($amount,0,'.',','));
			$this->exzel->getActiveSheet()->getStyle('D'.$jtotal)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
		
			$itotal = $row;
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, 'Khách hàng lẻ');
			$row++;
			$khtotal =0;
			$khamount = 0;
			foreach ($temp as $obj)
			{
				if($obj['tbname'] =='KH')
				{
					$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
					$this->exzel->getActiveSheet()->setCellValue('B'.$row, $obj['tbname']);
					$this->exzel->getActiveSheet()->setCellValue('C'.$row, number_format($obj['total'],2,'.',','));
					$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($obj['amount'],0,'.',','));
					$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
						
					$khtotal = 	$khtotal + $obj['total'];
					$khamount = $khamount + $obj['amount'];
					$stt++;
					$row++;
				}
			}
			$this->exzel->getActiveSheet()->setCellValue('C'.$itotal, number_format($khtotal,2,'.',','));
			$this->exzel->getActiveSheet()->setCellValue('D'.$itotal, number_format($khamount,0,'.',','));
			$this->exzel->getActiveSheet()->getStyle('D'.$itotal)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
		
			$row++;
			//change the font size
			//$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setSize(14);
			//make the font become bold
			//$this->exzel->getActiveSheet()->getStyle('A7')->getFont()->setBold(false);
			//merge cell A1 until D1
			//$this->exzel->getActiveSheet()->mergeCells('A7:A9');
			//set aligment to center for that merged cell (A1 to D1)
			//$this->exzel->getActiveSheet()->getStyle('A7')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				
			//$this->exzel->getActiveSheet()->getStyle('E7')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); //setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
				
			//$this->exzel->getActiveSheet()->mergeCells('B7:B9');
			//$this->exzel->getActiveSheet()->mergeCells('C7:C9');
			//$this->exzel->getActiveSheet()->mergeCells('D7:D9');
		}
		*/
		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	// View Báo cáo tổng hợp
	public function report_tong_hop($duration=null)
	{
		//echo $duration; exit();
		$return = '';
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('bdate');
			$type = strtoupper($this->input->post('type'));
			if($bdate == '')
			{
				$listdata	=	$this->taichinh->get_report_tong_hop($type);
				$listnguon =  $this->taichinh->get_report_tong_hop_nguon($type);
			}
			else
			{
				$duration = $type.'_'.$bdate;
				$listdata	=	$this->taichinh->get_report_tong_hop($duration);
				$listnguon =  $this->taichinh->get_report_tong_hop_nguon($duration);
			}
			
			if($listdata)
			{
				$return = '';
				$return = $this->generate_report($listdata,$listnguon);
			}
			echo json_encode(array('return_data'=>$return));
		}
		else 
		{
			$data['ItemID'] = 'User';
			$data['title'] = 'Báo cáo Tài chính tổng hợp';
			$data['description'] = 'Báo cáo Tài chính tổng hợp';
			$data['listdata']	=	$this->taichinh->get_report_tong_hop($duration);
			
			$data['listnguon'] =  $this->taichinh->get_report_tong_hop_nguon($duration);
			
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
			
			$this->_template['page']	= 'users/taichinh/rptonghop';
			$this->system_library->load($this->_template['page'], $data);
		}
	}
	
	public function generate_report($listdata,$listnguon)
	{
		$html = "";
		
		$html = $html . '<br/>
		<h3>Báo cáo quỹ phát sinh</h3><br />';
		if($listdata != null)
		{
			$html = $html . '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">
				<thead>
					<tr>
						<th class="ta-center">STT</th>
						<th>Tên hạng mục</th>
						<th class="ta-right">Số dư đầu kỳ</th>
						<th class="ta-right">Phát sinh tăng</th>
						<th class="ta-right">Phát sinh giảm</th>
						<th class="ta-right">Số dư cuối kỳ</th>
		
					</tr>
				</thead>';
			$html = $html . '<tbody>	
					<tr class="sum_f">
						<td class="ta-center">Tổng</td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
						<td></td>
					</tr>';
			$stt = 1;
			foreach($listdata as $data)
			{
					$html = $html . '<tr id="item_'.$data['id'].'">';
					$html = $html . '<td class="ta-center">'.$stt.'</td>';
					$stt++;
					$html = $html . '<td id="name_item_'.$data['id'].'">'. $data['item'] .'</td>';
					$html = $html . '<td class="ta-right">'. number_format($data['sddk']).'</td>';
					$html = $html . '<td id="ps_tang_'. $data['id'].'" class="ta-right">'. number_format($data['ps_tang']);
					$html = $html . '</td>';
					$html = $html . '<td id="ps_giam_'. $data['id'].'" class="ta-right">'.  number_format($data['ps_giam']);
					$html = $html . '</td>';
					$html = $html . '<td class="ta-right">'. number_format($data['sdck']).'</td>';
					$html = $html . '</tr>';
			}
			$html = $html . '</tbody>
				
			</table>
			<br/>
			<h3>Báo cáo theo hạng mục</h3><br />
			<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">
				<thead>
					<tr>
						<th class="ta-center">STT</th>
						<th>Tên hạng mục</th>
						<th class="ta-right">Phát sinh tăng</th>
						<th class="ta-right">Phát sinh giảm</th>
					</tr>
				</thead>';
				
				$html = $html . '<tbody>	
					<tr class="sum_f">
						<td class="ta-center">Tổng</td>
						<td></td>
						<td></td>
						<td></td>
					</tr>';
					 $stt = 1;
				foreach($listnguon as $data)
				{
					$html = $html . '<tr id="item_'. $data['id'].'">';
					$html = $html . '<td class="ta-center">'. $stt.'</td>';
					$stt++;
					$html = $html . '<td id="name_item_'. $data['id'].'">'.$data['name'].'</td>';
					$html = $html . '<td id="ps_tang_'. $data['id'].'" class="ta-right">'. number_format($data['ps_tang']).'</td>';
					$html = $html . '<td id="ps_giam_'. $data['id'].'" class="ta-right">'. number_format($data['ps_giam']).'</td>';
		
					$html = $html . '</tr>';
				}
				$html = $html . '</tbody></table>';
		
		}
		//$html = "";
		return $html;
	}
	public function xuat_report_chi_tiet($duration)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('begin_date');
			$edate = $this->input->post('end_date');
			$type = strtoupper($this->input->post('type'));
			if($bdate == '' || $edate == '')
			{
				if($type =='customize') $type ='THISMONTH';
				$data['listdata'] =	$this->taichinh->get_all_chung_tu_ok($type);
				$data['tong'] = $this->taichinh->tinh_tong_ps($type);
				
				$filename = 'bao_cao_chi_tiet_'.date('d-m-Y');
				$title = 'Báo cáo chi tiết '.date('d-m-Y');
			}
			else
			{
				$duration = $type.'_'.$bdate.'_'.$edate;
				$data['listdata']	=	$this->taichinh->get_all_chung_tu_ok($duration); 
				$data['tong'] = $this->taichinh->tinh_tong_ps($duration);
				$filename = 'bao_cao_tong_hop_'.date('d-m-Y');
				$title = 'Báo cáo tổng hợp từ ngày '. $bdate .' đến ngày '.$edate  ;
			}
			if($data)
			{
				$return = '';
				$return = $this->generate_excel_chi_tiet($data,$filename,$title);
			}
		
		}
	}
	
	public function generate_excel_chi_tiet($listdata,$filename,$title)
	{	
		//load our new PHPExcel library
		$this->load->library('exzel');
		//activate worksheet number 1
		$this->exzel->setActiveSheetIndex(0);
		//name the worksheet
		$this->exzel->getActiveSheet()->setTitle('Bao_Cao_chi_tiet');
		//set cell A1 content with some text
		$this->exzel->getActiveSheet()->setCellValue('A1', $title);
		
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setSize(18);
		//make the font become bold
		$this->exzel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);
		// Xử lý mảng
		$row = 6;
		$col = 1; //A col
		$data = $listdata['listdata'];
		$tong = $listdata['tong'];
		// Print Current Balance of System
		$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Báo cáo quỹ");
		// Chuyển xuống dòng tiếp theo row + 1 --> dong thu 7
		$row++;
		$this->exzel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
		$this->exzel->getActiveSheet()->getColumnDimension('C')->setWidth(25);
		$this->exzel->getActiveSheet()->getColumnDimension('A')->setWidth(5);
		$this->exzel->getActiveSheet()->getColumnDimension('D')->setWidth(25);
		$this->exzel->getActiveSheet()->getColumnDimension('E')->setWidth(25);
		if($data != null)
		{
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Báo cáo phát sinh trong kỳ");
			$row++; // chuyển đến dòng tiêp theo headline of area
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, "STT");
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Ngày tháng");
			$this->exzel->getActiveSheet()->setCellValue('C'.$row, "Tên hạng mục");
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, "Phát sinh tăng");
			$this->exzel->getActiveSheet()->setCellValue('E'.$row, "Phát sinh giảm");
			$row++; // chuyển đến dòng tiêp theo headline of area
			$this->exzel->getActiveSheet()->setCellValue('B'.$row, "Tổng");
			$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($tong[0]['tang']));
			$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($tong[0]['giam']));
			$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
			$this->exzel->getActiveSheet()->getStyle('E'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
			
			$row++; // chuyển đến dòng tiêp theo headline of area
			$stt = 1;
			foreach ($data as $item)
			{
				$this->exzel->getActiveSheet()->setCellValue('A'.$row, $stt);
				$this->exzel->getActiveSheet()->setCellValue('B'.$row, date('d/m/Y',strtotime($item['created_date'])));
				$this->exzel->getActiveSheet()->setCellValue('C'.$row, $item['item']);
				$this->exzel->getActiveSheet()->setCellValue('D'.$row, number_format($item['ps_tang']));
				$this->exzel->getActiveSheet()->setCellValue('E'.$row, number_format($item['ps_giam']));
				$this->exzel->getActiveSheet()->getStyle('D'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				$this->exzel->getActiveSheet()->getStyle('E'.$row)->getAlignment()->applyFromArray( array( 'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_RIGHT, 'vertical' => PHPExcel_Style_Alignment::VERTICAL_CENTER, 'rotation' => 0, 'wrap' => true ) );
				$stt++;
				$row++;
			}
		}
		else {
			$this->exzel->getActiveSheet()->setCellValue('A'.$row, "Không có phát sinh nào");
		}
		

		$filename=$filename.'.xls'; //save our workbook as this file name
		header('Content-Type: application/vnd.ms-excel'); //mime type
		header('Content-Disposition: attachment;filename="'.$filename.'"'); //tell browser what's the file name
		header('Cache-Control: max-age=0'); //no cache
			
		//save it to Excel5 format (excel 2003 .XLS file), change this to 'Excel2007' (and adjust the filename extension, also the header mime type)
		//if you want to save it as .XLSX Excel 2007 format
		$objWriter = PHPExcel_IOFactory::createWriter($this->exzel, 'Excel5');
		//force user to download the Excel file without writing it to server's HD
		//$objWriter->save("nameoffile.xls");
		$objWriter->save('php://output');
		exit();
	}
	// Xuất report chi tiết ra excel
	
	// Report Report chi tiết
	
	public function report_chitiet($duration=null)
	{
		if($duration == "CUSTOM")
		{
			$bdate = $this->input->post('bdate');
			$edate = $this->input->post('edate');
			$type = strtoupper($this->input->post('type'));
			if($bdate == '')
			{
				if($type =='customize') $type ='THISMONTH';
				$data['listdata'] =	$this->taichinh->get_all_chung_tu_ok($type);
				$data['tong'] = $this->taichinh->tinh_tong_ps($type);
			}
			else
			{
				$duration = $type.'_'.$bdate.'_'.$edate;
				$data['listdata']	=	$this->taichinh->get_all_chung_tu_ok($duration);
				$data['tong'] = $this->taichinh->tinh_tong_ps($duration);
			}
			
			if($data)
			{
				$return = '';
				$return = $this->generate_report_chitiet($data);
			}
			echo json_encode(array('return_data'=>$return));
		}
		else 
		{
			$data['ItemID'] = 'User';
		
			$data['title'] = 'Báo cáo Tài chính chi tiết';
			$data['description'] = 'Báo cáo Tài chính chi tiết';
			$this->load->helper('form');
			
			$data['listdata']	=	$this->taichinh->get_all_chung_tu_ok($duration);
			$data['tong'] = $this->taichinh->tinh_tong_ps($duration);
			
			$this->form_validation->set_rules('soluong', 'Số lượng', '');
			$this->form_validation->set_error_delimiters('', '<br />');
			
			$this->_template['page']	= 'users/taichinh/rpchitiet';
			$this->system_library->load($this->_template['page'], $data);
		}
	}
	
	public function generate_report_chitiet($arr)
	{
		$html = "";
		
		$listdata = $arr['listdata'];
		$tong = $arr['tong'];
		
		$html = $html . '<table class="table" cellpadding="0" cellspacing="0" id="list_chung_tu">
		<thead>
		<tr>
		<th>Ngày tháng</th>
		<th>Tên hạng mục</th>
		<th class="ta-right">Phát sinh tăng</th>
		<th class="ta-right">Phát sinh giảm</th>
		<th colspan="2"  class="ta-center">
		<span id="tip" class="functions text-center">Actions</span>
		</th>
		
		</tr>
		</thead>';
		$html = $html . '<tbody>	
					<tr class="sum_f">
						<td>Tổng</td>
						<td></td>';
		$html = $html . '<td class="ta-right">'. number_format($tong[0]['tang']).'</td>';
		$html = $html . '<td class="ta-right">'. number_format($tong[0]['giam']).'</td>';
		$html = $html . '<td></td>
					</tr>';
		$stt = 1;
		if($listdata != null)
		{
			foreach($listdata as $data):
				$html = $html . '<tr id="item_'. $data['id'].'">';
				$html = $html . '<td>'.date('d/m/Y',strtotime($data['created_date'])).'</td>';
				$html = $html . '<td id="name_item_'. $data['id'].'">'. $data['item'].'</td>';
				$html = $html . '<td id="ps_tang_'. $data['id'].'" class="ta-right">';
				$html = $html . '<input type="text" disabled="disabled" style="border: none; background-color:#FFFFFF; border-color: transparent;" id="hd_ps_tang_'. $data['id'].'" value="'.number_format($data['ps_tang']).'" />';
			
				$html = $html . '<div id="div_edit_ps_tang_'. $data['id'].'" style="display:none;">';
				$html = $html . '<input type="text" id="txt_ps_tang_'. $data['id'].'" value="'. number_format($data['ps_tang']).'" />';
				$html = $html . '</div>';
				$html = $html . '</td>';
				$html = $html . '<td id="ps_giam_'. $data['id'].'" class="ta-right">';
				$html = $html . '<input type="text" disabled="disabled" style="border: none; background-color:#FFFFFF; border-color: transparent;" id="hd_ps_giam_'. $data['id'].'" value="'.  number_format($data['ps_giam']).'" />';
			
				$html = $html . '<div id="div_edit_ps_giam_'. $data['id'].'" style="display:none;">';
				$html = $html . '<input type="text" id="txt_ps_giam_'. $data['id'].'" value="'. number_format($data['ps_giam']).'" />';
				$html = $html . '</div>
							</td>
							<td class="functions">';
						if($data['status']== 0):
							$html = $html . '<a id="act_edit_'. $data['id'].'" onmouseover="this.style.cursor=\'pointer\';" onclick="edit_item('. $data['id'].')" >Edit</a> | ';
							$html = $html . '<a id="act_submit_'. $data['id'].'" onmouseover="this.style.cursor=\'pointer\';" onclick="submit_item('. $data['id'].')" >Submit</a>';
							$html = $html . '<a id="act_save_'. $data['id'].'" onclick="save_item('. $data['id'].')"  style="display: none;">Save</a>';
							$html = $html . '<p id="dpl_'. $data['id'].'" style="display: none;">Đã có hiệu lực</p>';
						else:
							$html = $html . '<p id="dpl_'. $data['id'].'">Đã có hiệu lực</p>';
						endif;
				$html = $html . '</td>
			
						</tr>';
			endforeach;
		}
		$html = $html . '</tbody>';
				
		
		return $html;
	}
	// Submit chứng từ
	public function submit_chung_tu()
	{
		$id = $this->input->post('id');
		$return_id = $this->taichinh->submit_chung_tu($id);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	// Edit chứng từ
	public function save_chung_tu()
	{
		$id = $this->input->post('id');
		$ps_tang = $this->input->post('ps_tang');
		$ps_giam = $this->input->post('ps_giam');
		
		$return_id = $this->taichinh->save_chung_tu($id,$ps_tang,$ps_giam);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	// Thêm chứng từ
	public function them_chung_tu()
	{

		$hang_muc_id = $this->input->post('hang_muc_id');
		$ps_tang = $this->input->post('ps_tang');
		$ps_giam = $this->input->post('ps_giam');
		$note = $this->input->post('note');
		$nguon_goc_id = $this->input->post('nguon_goc_id');
		$return_id = $this->taichinh->themchungtu($hang_muc_id,$ps_tang,$ps_giam,$note,$nguon_goc_id);
		if($return_id)
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status,'return_id'=>$return_id));
	}
	
	public function submitsddk()
	{
		$id = $this->input->post('id');
		if($this->taichinh->submitsddk($id))
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status));
	}
	public function saveiteminstore()
	{
		$id = $this->input->post('id');
		$amount= $this->input->post('amount');
		if($this->taichinh->saveiteminstore($id,$amount))
		{
			$status = 1;
		}
		else
		{
			$status = 0;
		}
		echo json_encode(array('status' => $status));
	}
	//Lits
	public function viewsodudauky()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Số dư đầu kỳ';
		$data['description'] = 'Số dư đầu kỳ';
		$this->load->helper('form');
		
		$data['listdata']	=	$this->taichinh->get_all_sddk();
		
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$this->_template['page']	= 'users/taichinh/chitietdauky';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	function listchungtu()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Chứng từ Tài chính';
		$data['description'] = 'Chứng từ Tài chính';
		$this->load->helper('form');
		
		$data['listdata']	=	$this->taichinh->get_all_chung_tu();
		//var_dump($data['listdata'][0]);
		$listcates = $this->taichinh->get_all_category(1,0);
		$listsources = $this->taichinh->get_all_category(1,1);
		if($listcates != null)
		{
			foreach($listcates as $cate)
			{
				$drop[$cate['id']] = $cate['name'];
			}
		}
		else {
			$drop = null;
		}
		$source = array();
		if($listsources != null)
		{
			foreach($listsources as $cate)
			{
				$source[$cate['id']] = $cate['name'];
			}
		}
		else {
			$source = null;
		}
		//var_dump($drop);
		$data['drop'] = $drop;
		$data['source'] = $source;
		$this->form_validation->set_rules('soluong', 'Số lượng', '');
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$this->_template['page']	= 'users/taichinh/listchungtu';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	
	function hangmuc()
	{
		//$this->access->restrict('Member.Taichinh.Hangmuc');
		$data['ItemID'] = 'User';
		$data['title'] = 'Hạng mục Tài chính';
		$data['description'] = 'Hạng mục Tài chính';
		
		$this->access->restrict();
		$this->load->library('basic_library');
		$this->load->library('pagination');
		
		$config = array();
		$config['base_url'] = base_url().'member/taichinh/hangmuc';
		$config['total_rows'] = $this->taichinh->count_all_hangmuc();
		$config['per_page'] = 50;
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		$config['first_link'] = 'Đầu';
		$config['last_link'] = 'Cuối';
		$config['next_link'] = 'Tiến';
		$config['prev_link'] = 'Lùi';
		$this->pagination->initialize($config);
		$page = ($this->uri->segment(4))? $this->uri->segment(4):0;
		$data['sotrang']	=	$this->uri->segment(4);
		$data['listdata'] = $this->taichinh->get_all_hangmuc($config['per_page'], $page);
		$data['links'] = $this->pagination->create_links();
		$this->_template['page']	= 'users/taichinh/hangmuc_list';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	public function add_hang_muc()
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Thêm Hạng mục Tài chính';
		$data['description'] = 'Thêm Hạng mục Tài chính';
		$this->load->helper('form');
		$userid = $this->user->id;
		$lang_working = load_language_working();
	
		//here is validation form
		$this->form_validation->set_rules('name', 'Tên hạng mục', 'required|xss_clean|trim');
		$this->form_validation->set_rules('description', 'Mô tả', 'xss_clean|trim');
		$this->form_validation->set_error_delimiters('', '<br />');
	
		if ($this->form_validation->run() == TRUE)
		{
			$this->taichinh->add_hangmuc();
			$this->session->set_flashdata('message', lang('successfully_created'));
			redirect('member/taichinh/hangmuc/', 'refresh');
		}
	
		$this->_template['page']	= 'users/taichinh/hangmuc_add';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	//edit store
	public function edit_hang_muc($id)
	{
		$data['ItemID'] = 'User';
		$data['title'] = 'Sửa Hạng mục Tài chính';
		$data['description'] = 'Sửa Hạng mục Tài chính';
		$this->load->helper('form');
		$userid = $this->user->id;
		$lang_working = load_language_working();

		$data['listdata']	=	$this->taichinh->get_hangmuc($id);
		//here is validation form
		$this->form_validation->set_rules('name', 'Tên hạng mục', 'required|xss_clean|trim');
		$this->form_validation->set_rules('description', 'Mô tả', 'xss_clean|trim');
		$this->form_validation->set_error_delimiters('', '<br />');
	
		if ($this->form_validation->run() == TRUE)
		{
			$this->taichinh->edit_hangmuc($id);
			$this->session->set_flashdata('message', lang('successfully_created'));
			redirect('member/taichinh/hangmuc/', 'refresh');
		}
	
		$this->_template['page']	= 'users/taichinh/hangmuc_edit';
		$this->system_library->load($this->_template['page'], $data);
	}
	
	
	// Khóa Hạng mục
	public function submit_hang_muc()
	{
		$ids = $this->input->post('checked');
		$json = array();
		foreach($ids as $ar)
		{
			if($this->taichinh->submit_hang_muc($ar) == true)
			{
				$json[$ar]['result'] = 1;
				$json[$ar]['key'] = $ar;
			}
			else
			{
				$json[$ar]['result'] = 0;
				$json[$ar]['key'] = $ar;
			}
		}
	
		echo json_encode($json);
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/taichinh.php */